package org.aeon.utils;

import com.google.gson.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: MissingAeon
 * @date: 2024/1/30 11:41
 * @description:
 */
public class JsonUtils {

    /**
     * 读取list
     *
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> fromJsonArray(String json, Class<T> clazz) {
        List<T> lst = new ArrayList<>();

        try {
            JsonArray array = JsonParser.parseString(json).getAsJsonArray();
            for (JsonElement elem : array) {
                lst.add((new Gson()).fromJson(elem, clazz));
            }
        } catch (IllegalStateException ignored) {
        }
        return lst;
    }

}
